<template>
  <div class="translation-center-wrapper">
    <div class="translation-card">
      <h2 class="translation-title">文本翻译</h2>
      <div class="input-section">
        <textarea
          v-model="inputText"
          class="input-area"
          placeholder="请输入需要翻译的内容（支持中英自动识别）"
          :disabled="isTranslating"
          @keydown.enter.exact.prevent="handleTranslate"
        ></textarea>
        <div class="input-actions">
          <label style="margin-right: 0.5rem; color: #64748b;">目标语言：</label>
          <select v-model="targetLang" style="padding: 0.3rem 1rem; border-radius: 6px; border: 1px solid #e2e8f0; margin-right: 1rem;">
            <option v-for="opt in langOptions" :key="opt.value" :value="opt.value">{{ opt.label }}</option>
          </select>
          <button class="clear-btn" @click="clearInput" :disabled="isTranslating || !inputText">清空</button>
          <button class="translate-btn" @click="handleTranslate" :disabled="isTranslating || !inputText">
            <span v-if="!isTranslating">翻译</span>
            <span v-else class="loading-dots">
              <span>.</span><span>.</span><span>.</span>
            </span>
          </button>
        </div>
      </div>
      <div v-if="translatedText" class="result-section">
        <div class="result-header">
          <span>翻译结果</span>
          <button class="copy-btn" @click="copyResult">复制</button>
        </div>
        <div class="result-content scrollable-text">{{ translatedText }}</div>
      </div>
      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
const inputText = ref('')
const translatedText = ref('')
const isTranslating = ref(false)
const errorMessage = ref('')

// 支持的目标语言列表
const langOptions = [
  { label: '英文', value: 'en' },
  { label: '中文', value: 'zh' },
  { label: '日文', value: 'jp' },
  { label: '韩文', value: 'kor' },
  { label: '法文', value: 'fra' },
  { label: '德文', value: 'de' },
  { label: '西班牙文', value: 'spa' },
]
const targetLang = ref('en')

function clearInput() {
  inputText.value = ''
  translatedText.value = ''
  errorMessage.value = ''
}

async function handleTranslate() {
  if (!inputText.value.trim()) return
  isTranslating.value = true
  errorMessage.value = ''
  translatedText.value = ''
  try {
    const res = await axios.post('http://localhost:8000/translate', { text: inputText.value, to_lang: targetLang.value })
    translatedText.value = res.data.translated + `（${targetLang.value}）`
  } catch (e) {
    errorMessage.value = '翻译失败，请稍后重试'
  } finally {
    isTranslating.value = false
  }
}

function copyResult() {
  if (!translatedText.value) return
  navigator.clipboard.writeText(translatedText.value)
}
</script>

<style scoped>
.translation-center-wrapper {
  min-height: calc(100vh - 100px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: #f3f4f6;
  padding: 2rem 1rem 2rem 1rem;
  overflow-y: auto;
}
.translation-card {
  width: 100%;
  max-width: 700px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(0,0,0,0.08);
  padding: 2.2rem 2.5rem 1.2rem 2.5rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  align-items: center;
}
.translation-title {
  color: #4361ee;
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 1.2rem;
}
.input-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.input-area {
  width: 100%;
  min-height: 90px;
  max-height: 220px;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
  padding: 1rem;
  font-size: 1.1rem;
  color: #1a2a6c;
  background: #f8fafc;
  resize: vertical;
  transition: border 0.2s;
}
.input-area:focus {
  border: 1.5px solid #4361ee;
  outline: none;
}
.input-actions {
  display: flex;
  gap: 1.2rem;
  justify-content: flex-end;
}
.clear-btn {
  padding: 0.5rem 1.2rem;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  background: #fff;
  color: #64748b;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.clear-btn:disabled {
  color: #b0b8c9;
  cursor: not-allowed;
}
.clear-btn:not(:disabled):hover {
  background: #f3f4f6;
  color: #4361ee;
}
.translate-btn {
  padding: 0.5rem 1.8rem;
  border-radius: 8px;
  border: none;
  background: #4361ee;
  color: #fff;
  font-weight: 600;
  font-size: 1.1rem;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.translate-btn:disabled {
  background: #a0cfff;
  color: #fff;
  cursor: not-allowed;
}
.loading-dots {
  display: inline-flex;
  gap: 2px;
  font-size: 1.2em;
  color: #fff;
  letter-spacing: 1px;
  vertical-align: middle;
}
.loading-dots span {
  animation: blink 1.2s infinite both;
}
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}
.result-section {
  width: 100%;
  background: #f8fafc;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
  padding: 1.2rem 1.5rem;
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}
.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #4361ee;
  font-weight: 600;
  font-size: 1.1rem;
}
.copy-btn {
  padding: 0.3rem 1.1rem;
  border-radius: 8px;
  border: 1px solid #4361ee;
  background: #fff;
  color: #4361ee;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.copy-btn:hover {
  background: #4361ee;
  color: #fff;
}
.result-content {
  min-height: 40px;
  max-height: 240px;
  overflow-y: auto;
  font-size: 1.13rem;
  color: #1a2a6c;
  word-break: break-all;
  background: transparent;
  padding-right: 4px;
}
.scrollable-text {
  max-height: 240px;
  overflow-y: auto;
  word-break: break-all;
  padding-right: 4px;
}
.error-message {
  color: #f56c6c;
  background: #fee2e2;
  border-radius: 8px;
  padding: 0.7rem 1.2rem;
  margin-top: 1rem;
  text-align: center;
  font-size: 1rem;
}

@media (max-width: 900px) {
  .translation-card {
    max-width: 99vw;
    padding: 1.2rem 0.5rem;
  }
  .result-details-pair {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }
  .detail-block {
    min-width: auto;
    max-width: none;
  }
}

@media (max-width: 768px) {
  .translation-center-wrapper {
    padding: 1rem 0.5rem;
  }
  .translation-card {
    padding: 1rem;
  }
  .result-container {
    padding: 1.5rem 1rem;
  }
  .scrollable-text {
    max-height: 180px;
  }
}

@media (max-height: 600px) {
  .translation-center-wrapper {
    padding: 1rem 0.5rem;
  }
  .translation-card {
    padding: 1rem;
    gap: 1rem;
  }
  .result-container {
    padding: 1rem;
  }
  .scrollable-text {
    max-height: 120px;
  }
}
</style> 